package com.wanmait.gohome.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.wanmait.gohome.util.HttpUtil;
import com.wanmait.gohome.util.GsonUtils;
import com.wanmait.gohome.util.FileUtil;
import org.json.JSONArray;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 人脸对比
 */
public class FaceMatch {

    /**
     * 重要提示代码中所需工具类
     * FileUtil,Base64Util,HttpUtil,GsonUtils请从
     * https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
     * https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
     * https://ai.baidu.com/file/544D677F5D4E4F17B4122FBD60DB82B3
     * https://ai.baidu.com/file/470B3ACCA3FE43788B5A963BF0B625F3
     * 下载
     */
    public static String match(String filePath1,String filePath2) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/face/v3/match";
        try {

            byte[] bytes1 = FileUtil.readFileByBytes(filePath1);
            byte[] bytes2 = FileUtil.readFileByBytes(filePath2);
            String image1 = Base64Util.encode(bytes1);
            String image2 = Base64Util.encode(bytes2);

            List<Map<String, Object>> images = new ArrayList<>();

            Map<String, Object> map1 = new HashMap<>();
            map1.put("image", image1);
            map1.put("image_type", "BASE64");
            map1.put("face_type", "LIVE");
            map1.put("quality_control", "LOW");
            map1.put("liveness_control", "NORMAL");

            Map<String, Object> map2 = new HashMap<>();
            map2.put("image", image2);
            map2.put("image_type", "BASE64");
            map2.put("face_type", "LIVE");
            map2.put("quality_control", "LOW");
            map2.put("liveness_control", "NORMAL");

            images.add(map1);
            images.add(map2);

            String param = GsonUtils.toJson(images);

            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.c97731e6d301b604e1475447ea8403d8.2592000.1571466929.282335-16869526";

            String result = HttpUtil.post(url, accessToken, "application/json", param);
//            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    //测试
    public static void main(String[] args) {
        String filePath1 = "D:\\GoHome\\target\\GoHome\\web\\uploadImages\\14ec14ae-f1c5-430a-b08b-090d821f0245.jpg";
        String filePath2 = "D:\\GoHome\\target\\GoHome\\web\\uploadImages\\14ec14ae-f1c5-430a-b08b-090d821f0245.jpg";
        String result = FaceMatch.match(filePath1,filePath2);

        JsonObject jObject = new JsonParser().parse(result).getAsJsonObject();
        JsonElement jsonElement = jObject.get("result");
        float score = 0;
        if(!jsonElement.isJsonNull()){    //人脸对比出错的话，这里是空的                 节点判断是否为空
            score = jsonElement.getAsJsonObject().get("score").getAsFloat();
        }
        System.out.println(score+"*************");
    }
}